<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Formats a number as a currency string</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.metaphone.html">metaphone</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.nl-langinfo.html">nl_langinfo</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.money-format" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">money_format</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5)</p><p class="refpurpose"><span class="refname">money_format</span> &mdash; <span class="dc-title">Formats a number as a currency string</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.money-format-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>money_format</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
   , <span class="methodparam"><span class="type">float</span> <code class="parameter">$number</code></span>
   )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>money_format()</strong></span> returns a formatted version of
   <em><code class="parameter">number</code></em>.  This function wraps the C library
   function <span class="function"><strong>strfmon()</strong></span>, with the difference that
   this implementation converts only one number at a time.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.money-format-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">format</code></em></dt>

     <dd>

      <p class="para">
       The format specification consists of the following sequence:
       <ul class="itemizedlist">
        <li class="listitem"><p class="para">a <em>%</em> character</p></li>
        <li class="listitem"><p class="para">optional flags</p></li>
        <li class="listitem"><p class="para">optional field width</p></li>
        <li class="listitem"><p class="para">optional left precision</p></li>
        <li class="listitem"><p class="para">optional right precision</p></li>
        <li class="listitem"><p class="para">a required conversion character</p></li>
       </ul>
      </p>
      <p class="formalpara">
       <h5 class="title">Flags</h5>
       <p class="para">
       One or more of the optional flags below can be used:
        <dl>

         
          <dt>
<em>=</em><span class="replaceable">f</span></dt>

          <dd>

           <p class="para">
            The character <em>=</em> followed by a (single byte)
            character <span class="replaceable">f</span> to be used as the numeric fill
            character. The default fill character is space.
           </p>
          </dd>

         
         
          <dt>
<em>^</em></dt>

          <dd>

           <p class="para">
            Disable the use of grouping characters (as defined
            by the current locale).
           </p>
          </dd>

         
         
          <dt>
<em>+</em> or <em>(</em></dt>

          <dd>

           <p class="para">
            Specify the formatting style for positive and negative numbers.
            If <em>+</em> is used, the locale&#039;s equivalent for
            <em>+</em> and <em>-</em> will be used. If
            <em>(</em> is used, negative amounts are enclosed in
            parenthesis. If no specification is given, the default is
            <em>+</em>.
           </p>
          </dd>

         
         
          <dt>
<em>!</em></dt>

          <dd>

           <p class="para">
            Suppress the currency symbol from the output string.
           </p>
          </dd>

         
         
          <dt>
<em>-</em></dt>

          <dd>

           <p class="para">
            If present, it will make all fields left-justified (padded to the
            right), as opposed to the default which is for the fields to be
            right-justified (padded to the left).
           </p>
          </dd>

         
        </dl>

       </p>
      </p>
      <p class="formalpara">
       <h5 class="title">Field width</h5>
       <p class="para">
        <dl>

         
          <dt>
<span class="replaceable">w</span></dt>

          <dd>

           <p class="para">
            A decimal digit string specifying a minimum field width. Field will
            be right-justified unless the flag <em>-</em> is used.
            Default value is 0 (zero).
           </p>
          </dd>

         
        </dl>

       </p>
      </p>
      <p class="formalpara">
       <h5 class="title">Left precision</h5>
       <p class="para">
        <dl>

         
          <dt>
<em>#</em><span class="replaceable">n</span></dt>

          <dd>

           <p class="para">
            The maximum number of digits (<span class="replaceable">n</span>) expected
            to the left of the decimal character (e.g. the decimal point). It is
            used usually to keep formatted output aligned in the same columns,
            using the fill character if the number of digits is less than
            <span class="replaceable">n</span>. If the number of actual digits is
            bigger than <span class="replaceable">n</span>, then this specification is
            ignored.
           </p>
           <p class="para">
            If grouping has not been suppressed using the <em>^</em>
            flag, grouping separators will be inserted before the fill
            characters (if any) are added. Grouping separators will not be
            applied to fill characters, even if the fill character is a digit.
           </p>
           <p class="para">
            To ensure alignment, any characters appearing before or after the
            number in the formatted output such as currency or sign symbols are
            padded as necessary with space characters to make their positive and
            negative formats an equal length.
           </p>
          </dd>

         
        </dl>

       </p>
      </p>
      <p class="formalpara">
       <h5 class="title">
        Right precision
       </h5>
       <p class="para">
        <dl>

         
          <dt>
<em>.</em><span class="replaceable">p</span></dt>

          <dd>

           <p class="para">
            A period followed by the number of digits
            (<span class="replaceable">p</span>) after the decimal character. If the
            value of <span class="replaceable">p</span> is 0 (zero), the decimal
            character and the digits to its right will be omitted. If no right
            precision is included, the default will dictated by the current
            local in use. The amount being formatted is rounded to the specified
            number of digits prior to formatting.
           </p>
          </dd>

         
        </dl>

       </p>
      </p>
      <p class="formalpara">
       <h5 class="title">
        Conversion characters
       </h5>
       <p class="para">
        <dl>

         
          <dt>
<em>i</em></dt>

          <dd>

           <p class="para">
            The number is formatted according to the locale&#039;s international
            currency format (e.g. for the USA locale: USD 1,234.56).
           </p>
          </dd>

         
         
          <dt>
<em>n</em></dt>

          <dd>

           <p class="para">
            The number is formatted according to the locale&#039;s national
            currency format (e.g. for the de_DE locale: EU1.234,56).
           </p>
          </dd>

         
         
          <dt>
<em>%</em></dt>

          <dd>

           <p class="para">
            Returns the <em>%</em> character.
           </p>
          </dd>

         
        </dl>

       </p>
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">number</code></em></dt>

     <dd>

      <p class="para">
       The number to be formatted.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.money-format-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the formatted string. Characters before and after the formatting
   string will be returned unchanged.
   Non-numeric <em><code class="parameter">number</code></em> causes returning <strong><code>NULL</code></strong> and
   emitting <strong><code>E_WARNING</code></strong>.
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.money-format-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    The function <span class="function"><strong>money_format()</strong></span> is only defined if
    the system has strfmon capabilities.  For example, Windows does
    not, so <span class="function"><strong>money_format()</strong></span> is undefined in Windows.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    The <strong><code>LC_MONETARY</code></strong> category of the locale settings,
    affects the behavior of this function. Use <span class="function"><a href="function.setlocale.html" class="function">setlocale()</a></span>
    to set to the appropriate default locale before using this function.
   </p>
  </p></blockquote>   
 </div>


 <div class="refsect1 examples" id="refsect1-function.money-format-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4752">
    <p><strong>Example #1 <span class="function"><strong>money_format()</strong></span> Example</strong></p>
    <div class="example-contents"><p>
     We will use different locales and format specifications to
     illustrate the use of this function.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1234.56</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;let's&nbsp;print&nbsp;the&nbsp;international&nbsp;format&nbsp;for&nbsp;the&nbsp;en_US&nbsp;locale<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_MONETARY</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'en_US'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%i'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;USD&nbsp;1,234.56<br /><br />//&nbsp;Italian&nbsp;national&nbsp;format&nbsp;with&nbsp;2&nbsp;decimals`<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_MONETARY</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'it_IT'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%.2n'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;Eu&nbsp;1.234,56<br /><br />//&nbsp;Using&nbsp;a&nbsp;negative&nbsp;number<br /></span><span style="color: #0000BB">$number&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">1234.5672</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;US&nbsp;national&nbsp;format,&nbsp;using&nbsp;()&nbsp;for&nbsp;negative&nbsp;numbers<br />//&nbsp;and&nbsp;10&nbsp;digits&nbsp;for&nbsp;left&nbsp;precision<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_MONETARY</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'en_US'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%(#10n'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;($&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1,234.57)<br /><br />//&nbsp;Similar&nbsp;format&nbsp;as&nbsp;above,&nbsp;adding&nbsp;the&nbsp;use&nbsp;of&nbsp;2&nbsp;digits&nbsp;of&nbsp;right<br />//&nbsp;precision&nbsp;and&nbsp;'*'&nbsp;as&nbsp;a&nbsp;fill&nbsp;character<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%=*(#10.2n'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;($********1,234.57)<br /><br />//&nbsp;Let's&nbsp;justify&nbsp;to&nbsp;the&nbsp;left,&nbsp;with&nbsp;14&nbsp;positions&nbsp;of&nbsp;width,&nbsp;8&nbsp;digits&nbsp;of<br />//&nbsp;left&nbsp;precision,&nbsp;2&nbsp;of&nbsp;right&nbsp;precision,&nbsp;withouth&nbsp;grouping&nbsp;character<br />//&nbsp;and&nbsp;using&nbsp;the&nbsp;international&nbsp;format&nbsp;for&nbsp;the&nbsp;de_DE&nbsp;locale.<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_MONETARY</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'de_DE'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%=*^-14#8.2i'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1234.56</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;Eu&nbsp;1234,56****<br /><br />//&nbsp;Let's&nbsp;add&nbsp;some&nbsp;blurb&nbsp;before&nbsp;and&nbsp;after&nbsp;the&nbsp;conversion&nbsp;specification<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_MONETARY</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'en_GB'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$fmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'The&nbsp;final&nbsp;value&nbsp;is&nbsp;%i&nbsp;(after&nbsp;a&nbsp;10%%&nbsp;discount)'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$fmt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1234.56</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;final&nbsp;value&nbsp;is&nbsp;&nbsp;GBP&nbsp;1,234.56&nbsp;(after&nbsp;a&nbsp;10%&nbsp;discount)<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.money-format-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.setlocale.html" class="function" rel="rdfs-seeAlso">setlocale()</a> - Set locale information</span></li>
    <li class="member"><span class="function"><a href="function.sscanf.html" class="function" rel="rdfs-seeAlso">sscanf()</a> - 根据指定格式解析输入的字符</span></li>
    <li class="member"><span class="function"><a href="function.sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - Return a formatted string</span></li>
    <li class="member"><span class="function"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - 输出格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.number-format.html" class="function" rel="rdfs-seeAlso">number_format()</a> - 以千位分隔符方式格式化一个数字</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.metaphone.html">metaphone</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.nl-langinfo.html">nl_langinfo</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
